package com.itheima.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.common.PaginationVO;
import com.itheima.entity.Advimg;
import com.itheima.exception.BusinessException;
import com.itheima.mapper.AdvimgMapper;
import com.itheima.service.AdvimgService;
import com.itheima.vo.advVO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
@RequiredArgsConstructor
public class AdvimgServiceImpl implements AdvimgService {
    private final AdvimgMapper advimgMapper;

    @Override
    public PaginationVO<Advimg> page(String page, String limit, String advposdesc, String advimgpos) {
        PageHelper.startPage(Integer.parseInt(page), Integer.parseInt(limit));
        List<Advimg> cate = advimgMapper.getPageContent(advposdesc,advimgpos);
        PageInfo<Advimg> pageInfo = new PageInfo<>(cate);
        long count = pageInfo.getTotal();
        int totalPages = pageInfo.getPages();
        int pageSize = pageInfo.getPageSize();
        int currentPage = pageInfo.getNextPage();
        List<Advimg> advVOList = pageInfo.getList();

        PaginationVO<Advimg> paginationVO = new PaginationVO<>();
        paginationVO.setCount(count);
        paginationVO.setTotalPages(totalPages);
        paginationVO.setPageSize(pageSize);
        paginationVO.setCurrentPage(currentPage);
        paginationVO.setData(advVOList);
        return paginationVO;
    }

    @Override
    public void addAdvimg(Advimg adv) {
        adv.setDefault_data("1");
        advimgMapper.addAdvimg(adv);
    }

    @Override
    public Advimg rowInfoAdvpos(Integer id) {
        Advimg adv = advimgMapper.rowInfoAdvpos(id);
        return adv;
    }

    @Override
    public void deleteAdvimg(Integer id) {
        advimgMapper.deleteAdvimg(id);
    }

    @Override
    public void updateAdvimg(Advimg adv) {
        advimgMapper.updateAdvimg(adv);
    }
}
